package com.example.qrcodecamera;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class CSVDataViewActivity extends AppCompatActivity {
    private static final int PAGE_SIZE = 10;
    private int currentPage = 1;
    private CSVDataDbHelper dbHelper;
    private TableLayout tableLayout;
    private TextView tvPageInfo;
    private Button btnPrev, btnNext,btnback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_csvdata_view);

        dbHelper = new CSVDataDbHelper(this);
        tableLayout = findViewById(R.id.table_data);
        tvPageInfo = findViewById(R.id.tv_page_info);
        btnPrev = findViewById(R.id.btn_prev);
        btnNext = findViewById(R.id.btn_next);
        btnback =findViewById(R.id.btn_back);

        btnback.setOnClickListener(v->finish());

        btnPrev.setOnClickListener(v -> loadPage(currentPage - 1));
        btnNext.setOnClickListener(v -> loadPage(currentPage + 1));
        loadPage(1);

    }

    private void loadPage(int page) {
        int totalRecords = dbHelper.getTotalCount();
        int totalPages = (int) Math.ceil((double) totalRecords / PAGE_SIZE);
        if (page < 1 || page > totalPages) return;

        currentPage = page;
        tableLayout.removeAllViews();

        // 添加表头
        TableRow header = new TableRow(this);
        addHeaderCell(header, "货品编号");
        addHeaderCell(header, "货品批次号");
        addHeaderCell(header, "员工ID");
      //  addHeaderCell(header, "操作员");
        addHeaderCell(header, "毛重");
        addHeaderCell(header, "净重");
        addHeaderCell(header, "时间");
        tableLayout.addView(header);

        // 添加数据行
        List<CSVRecord> records = dbHelper.getPagedData(currentPage, PAGE_SIZE);
        for (CSVRecord record : records) {
            TableRow row = new TableRow(this);
            addDataCell(row, record.getMaterialId());
            addDataCell(row, record.getBatch());
            addDataCell(row, record.getStaffId());
            addDataCell(row, String.valueOf(record.getGrossWeight()));
            addDataCell(row, String.valueOf(record.getNetWeight()));
            addDataCell(row, record.getTimestamp());
            addDeleteButton(row, record); // 添加删除按钮
            tableLayout.addView(row);
        }

        // 更新分页信息
        tvPageInfo.setText(String.format("第 %d / %d 页", currentPage, totalPages));
        btnPrev.setEnabled(currentPage > 1);
        btnNext.setEnabled(currentPage < totalPages);
    }

    private void addHeaderCell(TableRow row, String text) {
        TextView tv = new TextView(this);
        tv.setText(text);
        tv.setBackgroundResource(R.drawable.table_header_bg);
        tv.setPadding(8, 8, 8, 8);
        row.addView(tv);
    }

    private void addDataCell(TableRow row, String text) {
        TextView tv = new TextView(this);
        tv.setText(text);
        tv.setPadding(8, 8, 8, 8);
        row.addView(tv);
    }

    // 新增：为每条记录添加删除按钮
    private void addDeleteButton(TableRow row, CSVRecord record) {
        Button btnDelete = new Button(this);
        btnDelete.setText("删除");
        btnDelete.setOnClickListener(v -> {
            new AlertDialog.Builder(this)
                    .setTitle("确认删除")
                    .setMessage("确定要删除这条记录吗？")
                    .setPositiveButton("确定", (dialog, which) -> {
                        new Thread(() -> {
                            boolean success = dbHelper.deleteRecord(record.getId());
                            runOnUiThread(() -> {
                                if (success) {
                                    Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                                    loadPage(currentPage); // 刷新当前页
                                } else {
                                    Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }).start();
                    })
                    .setNegativeButton("取消", null)
                    .show();
        });
        row.addView(btnDelete);
    }



}